<?php

/**
 * Link form.
 *
 * @package    25Mails
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class LinkForm extends BaseLinkForm {

    public function configure() {
        unset($this['template_id'], $this['created_at'], $this['updated_at'], $this['deleted_at']);
        $this->widgetSchema['name'] = new sfWidgetFormInputText(array(), array('readonly' => 'readonly'));
        $this->widgetSchema['target_url'] = new sfWidgetFormInputText();
        $this->widgetSchema['tags'] = new sfWidgetFormTextarea();
        $this->validatorSchema['tags'] = new sfValidatorPass();
        $link = $this->getObject();
        $tagString = '';
        foreach ($link->getTags() as $tag){
            $tagString .= $tag->getKeyword().' ';
        }
        $this->setDefault('tags', $tagString);

    }

    /**
     * Deletes and recreates tags for the Link object in this form
     * @param array $values
     */
    function updateObject($values = null) {
        $tagstring = $values['tags'];
        $tagstring = preg_replace('/\s+/', ' ', $tagstring);
        $tags = explode(' ',trim($tagstring));
        Doctrine_Core::getTable('Tag')->deleteTagsForLink($values['link_id']);
        foreach($tags as $tag){
            $t = new Tag();
            $t->setLinkId($values['link_id']);
            $t->setKeyword($tag);
            $t->save();
        }
        parent::updateObject($values);
    }

}
